<?php
class Admin_FaqsController extends Apps_Controller_Action {
	public function indexAction() {
		$this->_redirect('/admin/faqs/list-faqs');
	}

	public function listFaqsAction() {
		//  Action Delete
			$this->actResolve('act_delete',
					'delete', null, null, array('id' => $this->_getParam('item_checker')),
					true);

		$tbFaqs = new M_DbTable_Faqs();
		$grid = new Apps_Plugin_Grid('faqsGrid');
		// Set Grid
		$grid->setData($tbFaqs->getAllFaqs())
			 ->setTableCaption('List FAQS')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '1%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Question (Vi)',
			 			  'dataField'	=> 'question'),
			 		array('title'		=> 'Question (En)',
			 			  'dataField'	=> 'en_question'),
			 		array('title'		=> 'Faq category',
			 			  'dataField'	=> 'category_title'),
			 		array('title'      => 'Edit',
			 			  'html'       => array('<a href="/admin/faqs/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%'),
			 		array('title'      => 'Delete',
			 			  'html'       => array('<a href="/admin/faqs/delete?id=%s"><i class="icon-remove must-confirm " title="Xóa"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%')
			 		));
		$this->view->grid = $grid;

	}

	public function addAction() {
		$rq = $this->_request;

		$tbFaqs = new M_DbTable_Faqs();
		$f 	= new Admin_Form_AddFaqs();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbFaqs->safelyInsert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	public function editAction() {
		$rq = $this->_request;

		$faqsId = intval($rq->getParam('id'));
		$tbFaqs = new M_DbTable_Faqs();
		$f = new Admin_Form_AddFaqs();
		$f->setLegend('Sửa chi tiết FAQS');

		if(!$faqsId || !($r = $tbFaqs->fetchRow(array('id = ?' => $faqsId))->toArray())) {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbFaqs->safelyUpdate($f->getValues(), array('id = ?' => $faqsId))
					? self::$msgAddSuccess
					: self::$msgAddFailed);
				$this->_helper->redirector('index', null, null);
			}
		} else {
			$f->populate($r);
		}
		$f->setLegend('Edit Faqs');
		$this->view->form = $f;
		$this->render('add');
	}

	public function deleteAction() {
		$this->disableLayout(true, true);

		$rq = $this->_request;
		$faqsIds = $rq->getParam('id');
		$tbFaqs = new M_DbTable_Faqs();

		if(is_numeric($faqsIds)) {
			$this->_flashMessenger->addMessage(
						$tbFaqs->safelyDelete(array('id = ?' => $faqsIds)) ?
						self::$msgDeleteSuccess :
						self::$msgDeleteError
					);
			$this->goToActionIndex();
		} elseif (is_array($faqsIds)) {
			foreach ($faqsIds as $faqId) {
				$tbFaqs->safelyDelete(array('id = ?' => $faqId));
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
			$this->goToActionIndex();
		} else {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}
	}
}